//! Fichero: Singleton.h
//!
//! La clase Singleton permite evitar el uso directo de globales, siendo no obstante 
//! un concepto similar. Para utilizarla, se ha de heredar de ella.
//!
//! La clase proviene del proyecto RuneEmu.
//!


#pragma once

#include <assert.h>

#define SINGLETON_START(s) class s : public Singleton<s> { friend Singleton<s>;
#define SINGLETON_END }
#define SINGLETON_MAGIC(s) friend ZSingleton<s>;



namespace Shared
{


	template <class T> class Singleton
	{
	public:

		static void create()
		{
			if (instance == 0)
				instance = new T;
		}

		static void destroy()
		{
			delete instance;
			instance = 0;
		}

		static T & getInstance()
		{
			if (instance == 0) instance = new T;
			return *instance;
		}

		static T * getInstancePtr()
		{
			if(instance == 0) instance = new T;
			return instance;
		}

	protected:


		~Singleton()
		{

		}

		Singleton()
		{
			assert (instance == 0);
			instance = (T *) this;
		}


	private:

		Singleton(const Singleton& s)
		{
		}
		Singleton& operator=(const Singleton& s)
		{
			return *this;
		}

		static T * instance;
	};

	template <class T> T * Singleton<T>::instance = 0;

}




